package com.itheima.news.dao;

import com.itheima.news.pojo.News;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 新闻管理Dao
 * @author xiao1.wang@dmall.com
 * @date 2019-07-10 22:33:57
 */
public interface NewsDao {

    @Select("select * from t_news")
    List<News> selectAll();

    @Insert("insert into t_news(title, sub_title, img_url, sketch, content, behavior, create_time, update_time) values(#{title}, #{subTitle}, #{imgUrl}, #{sketch}, #{content}, #{behavior}, #{createTime}, #{updateTime})")
    int insert(News news);

    @Select("<script>" +
            "select * from t_news" +
            "<where>" +
            "<if test='news.title != null and news.title != \"\"'>" +
            "<![CDATA[ and title = #{news.title} ]]>" +
            "</if>" +
            "<if test='news.subTitle != null and news.subTitle != \"\"'>" +
            "<![CDATA[ and sub_title = #{news.subTitle} ]]>" +
            "</if>" +
            "<if test='news.sketch != null and news.sketch != \"\"'>" +
            "<![CDATA[ and sketch like '%${news.sketch}%' ]]>" +
            "</if>" +
            "</where>" +
            "limit #{start}, #{size}" +
            "</script>")
    List<News> selectByCondition(@Param("news") News news, @Param("start") Integer start, @Param("size") Integer size);

    @Select("<script>" +
            "select count(*) from t_news" +
            "<where>" +
            "<if test='news.title != null and news.title != \"\"'>" +
            "<![CDATA[ and title = #{news.title} ]]>" +
            "</if>" +
            "<if test='news.subTitle != null and news.subTitle != \"\"'>" +
            "<![CDATA[ and sub_title = #{news.subTitle} ]]>" +
            "</if>" +
            "<if test='news.sketch != null and news.sketch != \"\"'>" +
            "<![CDATA[ and sketch like '%${news.sketch}%' ]]>" +
            "</if>" +
            "</where>" +
            "</script>")
    Integer selectCount(@Param("news") News news);
}
